package com.indigon.parkStory.control
{
	import com.indigon.parkStory.model.objects.ScoreModel;
	import com.indigon.parkStory.view.SoundMain;
	import com.indigon.utils.SoundManager;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import mx.resources.ResourceManager;

	public class SummaryScreen
	{
		public static const SUCCESS_DELAY : uint = 1000;
		public static const FAILURE_DELAY : uint = 500;
		
		private static const INITAL_SUCCESS_SCORE_DELAY : uint = 500;
		private static const SUCCESS_SCORE_DELAY        : uint = 500;
		
		private var _control       : ControlMain;
		private var _stage         : Stage;
		private var _victoryScreen : MovieClip;
		private var _defeatScreen  : MovieClip;
		private var _current       : MovieClip;
		private var _timeout       : Number;
		private var _score		   : ScoreModel;
		private var _starCount   : uint;
		
		public function SummaryScreen(control:ControlMain, stage:Stage, centerOffset:Point)
		{
			this._stage   = stage;
			this._control = control;
			
			_victoryScreen = new GFX_LevelSuccess();
			_defeatScreen  = new GFX_LevelFailure();
			
			_victoryScreen.stop();
			_defeatScreen.stop();
			
			_victoryScreen.x = centerOffset.x;
			_victoryScreen.y = centerOffset.y;
			_defeatScreen.x  = centerOffset.x;
			_defeatScreen.y  = centerOffset.y;

			_defeatScreen["txt_title"].text = ResourceManager.getInstance().getString("ui", "summary_failure_title");
			_victoryScreen["txt_title"].text = ResourceManager.getInstance().getString("ui", "summary_success_title");
			_victoryScreen["txt_base"].text = ResourceManager.getInstance().getString("ui", "summary_success_score");
			_victoryScreen["txt_combo"].text = ResourceManager.getInstance().getString("ui", "summary_success_combo");
			_victoryScreen["txt_time"].text  = ResourceManager.getInstance().getString("ui", "summary_success_time");
			
			resetVictoryScreen();
		}
		
		private function resetVictoryScreen():void
		{
			_victoryScreen["score"].text = "000000";
			_victoryScreen["time"].text  = "000000";
			_victoryScreen["combo"].text = "000000";
			
			_victoryScreen["final"].text = "000000";
			_victoryScreen["final_shadow"].text = "000000";
			
			_victoryScreen["star_0"].gotoAndStop(1);
			_victoryScreen["star_1"].gotoAndStop(1);
			_victoryScreen["star_2"].gotoAndStop(1);
			
			_score = null;
			_starCount = 0;
		}
		
		public function showVictory(score:ScoreModel):void
		{
			this._score = score;
			
			_stage.addChild(_victoryScreen);
			_current = _victoryScreen;
			
			_victoryScreen["btn_index"].addEventListener(MouseEvent.MOUSE_DOWN, pressIndex);
			_victoryScreen["btn_replay"].addEventListener(MouseEvent.MOUSE_DOWN, pressReplay);
			_victoryScreen["btn_next"].addEventListener(MouseEvent.MOUSE_DOWN, pressNext);
			
			_timeout = setTimeout(showBaseScore, INITAL_SUCCESS_SCORE_DELAY);
		}
		
		private function showBaseScore():void
		{
			_timeout = setTimeout(showComboScore, SUCCESS_SCORE_DELAY);
			
			SoundManager.instance.playSoundByClass(SFX_SummaryScore);
			
			setScoreText(_victoryScreen["score"], _score.score);
			setScoreText(_victoryScreen["final"], _score.score);
			setScoreText(_victoryScreen["final_shadow"], _score.score);
			
			SoundManager.instance.playSoundByClass(SoundMain.SFX_StarLevel[_starCount]);
			_victoryScreen["star_" + _starCount].gotoAndPlay("yes");
			
			_starCount++;
		}
		
		private function showComboScore():void
		{
			_timeout = setTimeout(showTimeScore, SUCCESS_SCORE_DELAY);
			
			setScoreText(_victoryScreen["combo"], _score.comboScore);
			setScoreText(_victoryScreen["final"], _score.score + _score.comboScore);
			setScoreText(_victoryScreen["final_shadow"], _score.score + _score.comboScore);
			
			if (_score.willGetComboBonus())
			{
				SoundManager.instance.playSoundByClass(SoundMain.SFX_StarLevel[_starCount]);
				_victoryScreen["star_" + _starCount].gotoAndPlay("yes");
			}
			else
			{
				SoundManager.instance.playSoundByClass(SoundMain.SFX_StarBadLevel[_starCount]);
				_victoryScreen["star_" + _starCount].gotoAndPlay("no");
			}
			_starCount++;
		}
		
		private function showTimeScore():void
		{
			_timeout = NaN;
			
			setScoreText(_victoryScreen["time"], _score.timeScore);
			setScoreText(_victoryScreen["final"], _score.score + _score.comboScore + _score.timeScore);
			setScoreText(_victoryScreen["final_shadow"], _score.score + _score.comboScore + _score.timeScore);
			
			if (_score.willGetTimeBonus())
			{
				SoundManager.instance.playSoundByClass(SoundMain.SFX_StarLevel[_starCount]);
				_victoryScreen["star_" + _starCount].gotoAndPlay("yes");
			}
			else
			{
				SoundManager.instance.playSoundByClass(SoundMain.SFX_StarBadLevel[_starCount]);
				_victoryScreen["star_" + _starCount].gotoAndPlay("no");
			}
			_starCount++;
		}
		
		public function showDefeat():void
		{
			_stage.addChild(_defeatScreen);
			_current = _defeatScreen;
			
			_defeatScreen["btn_index"].addEventListener(MouseEvent.MOUSE_DOWN, pressIndex);
			_defeatScreen["btn_replay"].addEventListener(MouseEvent.MOUSE_DOWN, pressReplay);
		}
		
		private function hide():void
		{
			_stage.removeChild(_current);
			
			if (_current == _victoryScreen)
			{
				resetVictoryScreen();
				_victoryScreen["btn_index"].removeEventListener(MouseEvent.MOUSE_DOWN, pressIndex);
				_victoryScreen["btn_replay"].removeEventListener(MouseEvent.MOUSE_DOWN, pressReplay);
				_victoryScreen["btn_next"].removeEventListener(MouseEvent.MOUSE_DOWN, pressNext);
			}
			else
			{
				_defeatScreen["btn_index"].removeEventListener(MouseEvent.MOUSE_DOWN, pressIndex);
				_defeatScreen["btn_replay"].removeEventListener(MouseEvent.MOUSE_DOWN, pressReplay);
			}
			_current = null;
		}
		
		private function pressIndex(e:Event=null):void
		{
			resetTimeout();
			SoundManager.instance.playSoundByClass(SFX_UI);
			hide();
			_control.gotoIndexScreen();
			e.stopImmediatePropagation();
		}
		
		private function pressReplay(e:Event=null):void
		{
			resetTimeout();
			SoundManager.instance.playSoundByClass(SFX_UI);
			hide();
			_control.restartSession();
			e.stopImmediatePropagation();
		}
		
		private function pressNext(e:Event=null):void
		{
			resetTimeout();
			SoundManager.instance.playSoundByClass(SFX_UI);
			hide();
			_control.gotoNextLevel();
			e.stopImmediatePropagation();
		}
		
		private function resetTimeout():void
		{
			if (!isNaN(_timeout))
			{
				clearTimeout(_timeout);
				_timeout = NaN;
			}
		}
		
		private function setScoreText(field:TextField, score:uint):void
		{
			field.text = score.toString();
		}
	}
}